#include "page2.h"

#include <QPushButton>
#include <QFileDialog>
#include <QFileInfo>
#include <QMessageBox>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPalette>


TestPage2::TestPage2(QWidget *parent) : QWidget(parent)
{
    printf("Create page 2.\n");

    file_path = "../../res";

    QPushButton *btn_open = new QPushButton("&Open");
    btn_open->setFixedWidth(QWidget::fontMetrics().width(btn_open->text()));
                                            /* 设置按钮宽度自适应文字宽度 */
    connect(btn_open, &QPushButton::pressed, this, &TestPage2::open_file);
    edt_file_path = new QLineEdit("../../res/01-1.jpg");
    edt_file_path->setPlaceholderText("The path of image file");
    edt_file_path->setTextMargins(0, 0, btn_open->width(), 0);
    QLabel *lbl_file= new QLabel("&Image File:");
    lbl_file->setFixedWidth(QWidget::fontMetrics().width(lbl_file->text()));
    lbl_file->setBuddy(edt_file_path);
    QPushButton *btn_show = new QPushButton("&Show");
    connect(btn_show, &QPushButton::pressed, this, &TestPage2::show_image);
    btn_show->setFixedWidth(QWidget::fontMetrics().width(btn_show->text()));
    QPushButton *btn_clear = new QPushButton("&Clear");
    connect(btn_clear, &QPushButton::pressed, this, &TestPage2::clear_image);
    btn_clear->setFixedWidth(QWidget::fontMetrics().width(btn_clear->text()));
    edt_info = new QTextEdit();
    edt_info->setReadOnly(true);
    edt_log = new QTextEdit("log");
    edt_log->setReadOnly(true);
    viewer = new ImageViewer(); /* TODO@huanlf */
    // viewer->setWidgetResizable(true);   /* BUG?: 启用后左右对齐设置失效 */

    QHBoxLayout *hbl_ins_btn = new QHBoxLayout();
    hbl_ins_btn->setMargin(0);
    hbl_ins_btn->setSpacing(0);
    hbl_ins_btn->addWidget(btn_open);
    hbl_ins_btn->setAlignment(btn_open, Qt::AlignRight);
    edt_file_path->setLayout(hbl_ins_btn);
    QHBoxLayout *hbl_loader = new QHBoxLayout();
    hbl_loader->setMargin(0);
    hbl_loader->setSpacing(2);
    hbl_loader->addWidget(lbl_file, 0, Qt::AlignRight);
    hbl_loader->addWidget(edt_file_path);
    hbl_loader->addWidget(btn_show);
    hbl_loader->addWidget(btn_clear);
    QVBoxLayout *vbl_msg = new QVBoxLayout();
    vbl_msg->setMargin(0);
    vbl_msg->setSpacing(2);
    vbl_msg->addWidget(edt_info);
    vbl_msg->addWidget(edt_log);
    QVBoxLayout *vbl_img = new QVBoxLayout();
    vbl_img->setMargin(0);
    vbl_img->setSpacing(2);
    vbl_img->addWidget(viewer);
    QHBoxLayout *hbl_disper = new QHBoxLayout();
    hbl_disper->setMargin(0);
    hbl_disper->setSpacing(2);
    hbl_disper->addLayout(vbl_img);
    hbl_disper->addLayout(vbl_msg);
    QVBoxLayout *mainbox = new QVBoxLayout();
    mainbox->setMargin(4);
    mainbox->setSpacing(2);
    mainbox->addLayout(hbl_loader, 1);
    mainbox->addLayout(hbl_disper, 1);
    setLayout(mainbox);
}

void TestPage2::open_file(void)
{
    printf("<Open> pressed to select file.\n");
    QString file = QFileDialog::getOpenFileName(this, tr("open image"),
            file_path, tr("Image files(*)"));
    if (file.length() == 0) {
        printf("No file selected.\n");
        return;
    }
    printf("select file: %s.\n", file.toStdString().data());
    edt_file_path->setText(file);
    QFileInfo fi = QFileInfo(file);
    file_path = fi.absolutePath();
    printf("set file path: %s.\n", file_path.toStdString().data());
}

void TestPage2::show_image(void)
{
    QString filename = edt_file_path->text();
    printf("show image file: %s.\n", filename.toStdString().data());

    QImage image;
    if (image.load(filename) == false) {
        QString msg = tr("Load failed.\nInvalid image file: ") + filename;
        QMessageBox::warning(this, tr("Error"), msg);
        return;
    }
    viewer->set_pixmap(QPixmap::fromImage(image));

    QVector<MarkPoint> *poi = viewer->points();
    poi->push_back(MarkPoint(0, 0, QColor(0xFF, 0, 0), 4));
    poi->push_back(MarkPoint(8, 8, QColor(0xFF, 0, 0), 4));
    poi->push_back(MarkPoint(16, 16, QColor(0xFF, 0, 0), 4));
    poi->push_back(MarkPoint(32, 32, QColor(0xFF, 0, 0), 4));
    poi->push_back(MarkPoint(64, 64, QColor(0xFF, 0, 0), 4));
    poi->push_back(MarkPoint(128, 128, QColor(0xFF, 0, 0), 4));
    QVector<MarkLine> *lin = viewer->lines();
    lin->push_back(MarkLine(QLine(74, 117, 98, 117), QColor(0, 0xFF, 0), 1));
    lin->push_back(MarkLine(QLine(74, 117, 74, 142), QColor(0, 0xFF, 0), 1));
    lin->push_back(MarkLine(QLine(98, 117, 98, 142), QColor(0, 0xFF, 0), 1));
    lin->push_back(MarkLine(QLine(74, 142, 98, 142), QColor(0, 0xFF, 0), 1));

    update_info();
}

void TestPage2::clear_image(void)
{
    printf("clear image.\n");
    viewer->set_pixmap();

    update_info();
}

void TestPage2::update_info(void)
{
    edt_info->clear();
    QRect rect_main = parentWidget()->parentWidget()->parentWidget()->
        geometry();
    QPoint poi_main = parentWidget()->parentWidget()->parentWidget()->pos();
    edt_info->append(QString().sprintf("window:\t%dx%d\tpos:%d,%d",
                rect_main.width(), rect_main.height(),
                poi_main.x(), poi_main.y()));
    QRect rect = viewer->geometry();
    edt_info->append(QString().sprintf("area:\t%dx%d",
                rect.width(), rect.height()));
    if (!viewer->is_empty()) {
        QSize size = viewer->pixmap()->size();
        edt_info->append(QString().sprintf("image size:\t%dx%d",
                    size.width(), size.height()));
        edt_info->append(QString().sprintf("image scale:\t%f",
                    viewer->scale()));
    }
}

